﻿using System.Collections.Generic;
using offline2048;
using UnityEngine;
using pooling;
using TMPro;

public class Cell : PoolingObject
{
    private int linkCellListIndex = -1;
    private int row = 0;
    private int column = 0;

    private SpriteRenderer sp;
    private TextMeshPro numberText;
    private GameObject gem;
    private GameObject doller;
    public bool IsDestroyed = false;
    private bool hasGem = false;

    private void Awake()
    {
        sp = GetComponent<SpriteRenderer>();
        numberText = transform.GetChild(0).GetComponent<TextMeshPro>();
        gem = transform.GetChild(1).gameObject;
        doller = transform.GetChild(2).gameObject;
    }

    public void InitCell(Color _color,int _number,Vector3 _startPosition, Vector3 _position,  int _column, int _row, bool isCurrent)
    {
        Row = _row;
        Column = _column;
        LinkCellListIndex = -1;
        IsDestroyed = false;
        hasGem = false;
        SetNumber(_number);

        sp.color = _color;
        transform.position = _startPosition;

        if(isCurrent)
        {
            LeanTween.move(gameObject, _position, 0.2f).setEase(LeanTweenType.easeInCubic).setOnComplete(() => {
                GameManager.instance.OnCurrentCellStop(Column, Row);
            });
        }
        else
        {
            LeanTween.move(gameObject, _position, 0.2f).setEase(LeanTweenType.easeInCubic);
        }

    }
    
    public void AttachGem()
    {
        if (UserLayerManager.Instance.IsOrganic() || UserLayerManager.Instance.GetCurrentUserLayer() == UserLayerManager.UserLayerType.None)
        {
            gem.SetActive(true);
            doller.SetActive(false);   
        }
        else
        {
            gem.SetActive(false);
            doller.SetActive(true);
        }
        hasGem = true;
    }

    public void GainNumber(int gain)
    {
        SetNumber(Number + gain);
        ParticleManager.instance.SpawnBurstParticle(transform.position, sp.color);

        if (hasGem)
        {
            ParticleManager.instance.PlayCoinAnimation(transform.position);
            GameManager.instance.CurrentData.CoinNumber += 10;
            hasGem = false;
            gem.SetActive(false);
        }
    }

    public void SetNumber(int number)
    {
        Number = number;
        if(Number > 109)
        {
            Number = 1;
        }

        numberText.text = Utility.CellNumber(Number);
        sp.color = GameManager.instance.GetColorByNumber(Number);
    }

    public void UpFillCell(int row,Vector3 position)
    {
        Row = row;
        LinkCellListIndex = -1;
        transform.position = position;
    }

    public void DownFillCell(Vector3 position)
    {
        Row++;
        transform.position = position;
    }

    public void FusionCell(Vector3 position,float releaseTime)
    {
        if (hasGem)
        {
            ParticleManager.instance.PlayCoinAnimation(transform.position);
            GameManager.instance.CurrentData.CoinNumber += 10;
        }

        LeanTween.move(gameObject, position, releaseTime).setOnComplete(() => {

            AudioController.Play("Burst");
            GameManager.instance.OnCellFusion(column, row);
            OnRelease();

        });
    }

    public override void OnRelease()
    {
        base.OnRelease();

        IsDestroyed = true;
        hasGem = false;
        LinkCellListIndex = -1;
        
        gem.SetActive(false);
    }

    #region
    public int Number
    {
        get;
        set;
    } = -1;
        
    public int LinkCellListIndex
    {
        get { return linkCellListIndex; }
        set { linkCellListIndex = value; }
    }

    public int Row
    {
        get { return row; }
        set { row = value; }
    }

    public int Column
    {
        get { return column; }
        set
        {
            column = value;
        }
    }
    #endregion
}
